package com.bykj.taketurns.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.HomeActivity;
import com.bykj.taketurns.findfragment.GlideImageLoader;
import com.bykj.taketurns.findfragment.adapter.FindRelaAdapter;
import com.bykj.taketurns.findfragment.bean.FindBean;
import com.bykj.taketurns.utils.Constant;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/8/9.
 */

public class FindFragment extends Fragment {
    private HomeActivity activity;
    private View mRootView;
    private Banner banner_find;
    private ArrayList<Integer> integers;
    private RecyclerView rv_find_data;
    private FindBean findBean;
    private FindRelaAdapter findRelaAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_find, container, false);
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initHeader();
        initWidget();
        setWidgetState();
    }

    private void initData() {

        integers = new ArrayList<>();
        integers.add(R.mipmap.ic_launcher_round);
        integers.add(R.mipmap.ic_launcher_round);
        integers.add(R.mipmap.ic_launcher_round);

    }

    private void initHeader() {

    }

    private void initWidget() {

        banner_find = mRootView.findViewById(R.id.banner_find);
        rv_find_data = mRootView.findViewById(R.id.rv_find_data);

    }

    private void setWidgetState() {

        rv_find_data.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_find_data.setHasFixedSize(true);
        rv_find_data.setItemAnimator(new DefaultItemAnimator());
        findRelaAdapter = new FindRelaAdapter();

    }

    //进行网络请求数据
    public void postData() {
        OkGo.get(Constant.JHC_SERVICE + Constant.ServiceConstant.FIND_URL)     // 请求方式和请求url
                .tag(this)
                .execute(new StringCallback() {


                    @Override
                    public void onSuccess(String s, Call call, Response response) {

                        findBean = activity.gson.fromJson(s, FindBean.class);

                        findRelaAdapter.addDatas(getActivity(),findBean.getTypeList());

                        rv_find_data.setAdapter(findRelaAdapter);

                        setHeader(rv_find_data);

                    }
                });
    }

    //添加头部banner
    private void setHeader(RecyclerView view) {
        View header = LayoutInflater.from(getActivity()).inflate(R.layout.find_header_item, view, false);
        banner_find = header.findViewById(R.id.banner_find);
        findRelaAdapter.setHeaderView(header);
        //设置banner样式
        banner_find.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner_find.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner_find.setImages(integers);
        //设置自动轮播，默认为true
        banner_find.isAutoPlay(true);
        //设置轮播时间
        banner_find.setDelayTime(3000);
        //设置指示器位置
        banner_find.setIndicatorGravity(BannerConfig.RIGHT);
        banner_find.start();

    }

    @Override
    public void onResume() {
        super.onResume();
        postData();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (HomeActivity) context;
    }
}
